<!--
 * @Description:
 * @Version: 1.0
 * @Author: liguansheng
 * @Date: 2022-08-24 14:14:29
 * @LastEditors: liguansheng
 * @LastEditTime: 2022-08-26 10:51:08
-->
<template>
  <el-form
    :model="form"
    :rules="rules"
    label-width="120px"
    label-position="top"
  >
    <el-form-item
      label="客户端Id"
      prop="clientId"
      v-if="props.type === 'add'"
    >
      <el-input
        v-model="form.clientId"
      />
    </el-form-item>
    <el-form-item
      label="客户端名称"
      prop="clientName"
    >
      <el-input
        v-model="form.clientName"
      />
    </el-form-item>
    <el-form-item
      label="描述"
      prop="description"
    >
      <el-input
        v-model="form.description"
      />
    </el-form-item>
    <el-form-item
      label="客户端Uri"
      prop="clientUri"
    >
      <el-input
        v-model="form.clientUri"
      />
    </el-form-item>
    <el-form-item
      label="Logo Uri"
      prop="logoUri"
    >
      <el-input
        v-model="form.logoUri"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="requireConsent"
    >
      <el-checkbox
        v-model="form.requireConsent"
        label="需要许可"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="requireRequestObject"
      v-if="props.type === 'edit'"
    >
      <el-checkbox
        v-model="form.requireRequestObject"
        label="需要请求对象"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="allowRememberConsent"
      v-if="props.type === 'edit'"
    >
      <el-checkbox
        v-model="form.allowRememberConsent"
        label="允许记住许可"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="enabled"
      v-if="props.type === 'edit'"
    >
      <el-checkbox
        v-model="form.enabled"
        label="启用"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="allowOfflineAccess"
      v-if="props.type === 'edit'"
    >
      <el-checkbox
        v-model="form.allowOfflineAccess"
        label="允许离线访问"
      />
    </el-form-item>
    <el-form-item
      label="Front-Channel注销URL"
      prop="frontChannelLogoutUri"
      v-if="props.type === 'edit'"
    >
      <el-input
        v-model="form.frontChannelLogoutUri"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="frontChannelLogoutSessionRequired"
      v-if="props.type === 'edit'"
    >
      <el-checkbox
        v-model="form.frontChannelLogoutSessionRequired"
        label="需要Front-Channel注销会话"
      />
    </el-form-item>
    <el-form-item
      label="Back-Channel注销URL"
      prop="backChannelLogoutUri"
      v-if="props.type === 'edit'"
    >
      <el-input
        v-model="form.backChannelLogoutUri"
      />
    </el-form-item>
    <el-form-item
      label=""
      prop="backChannelLogoutSessionRequired"
      v-if="props.type === 'edit'"
    >
      <el-checkbox
        v-model="form.backChannelLogoutSessionRequired"
        label="需要Back-Channel注销会话"
      />
    </el-form-item>
    <el-form-item
      label="允许的身份令牌签名算法"
      prop="allowedIdentityTokenSigningAlgorithms"
      v-if="props.type === 'edit'"
    >
      <el-input
        v-model="form.allowedIdentityTokenSigningAlgorithms"
      />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { IElFormRule } from '@/types/element-plus'
import { PropType, ref, watch } from 'vue'
// apiResources: [{displayName: "11", name: "11", left: true}, {displayName: "22", name: "22", left: true}]
// callbackUrl: "回调 Url"
// identityResources: [{displayName: "Your user identifier", name: "openid", left: true},…]
// logoutUrl: "Logout Url"
// scopes: ["openid", "role", "11", "22"]
// secrets: [{type: "SharedSecret", value: "值", description: "描述", expiration: "2022-08-04T04:00:00.000Z"}]
const props = defineProps({
  modelValue: {
    type: Object as PropType<{
      clientId: string
      clientName: string
      description: string
      clientUri: string
      logoUri: string
      requireConsent: boolean
      requireRequestObject: boolean
      allowRememberConsent: boolean
      enabled: boolean
      allowOfflineAccess: boolean
      frontChannelLogoutUri: string
      frontChannelLogoutSessionRequired: boolean
      backChannelLogoutUri: string
      backChannelLogoutSessionRequired: boolean
      allowedIdentityTokenSigningAlgorithms: string
    }>,
    required: true
  },
  type: {
    type: String as PropType<'add' | 'edit'>,
    required: true
  }
})
interface EmitsType {
  (e: 'update:modelValue', form: {
      clientId: string
      clientName: string
      description: string
      clientUri: string
      logoUri: string
      requireConsent: boolean
      requireRequestObject: boolean
      allowRememberConsent: boolean
      enabled: boolean
      allowOfflineAccess: boolean
      frontChannelLogoutUri: string
      frontChannelLogoutSessionRequired: boolean
      backChannelLogoutUri: string
      backChannelLogoutSessionRequired: boolean
      allowedIdentityTokenSigningAlgorithms: string
    }): void
}
const emit = defineEmits<EmitsType>()

const form = ref(props.modelValue)
const rules = ref<IElFormRule>({
  clientId: [
    { required: true, message: '客户端Id不能为空', trigger: 'blur' }
  ],
  clientName: [
    { required: true, message: '客户端名称不能为空', trigger: 'blur' }
  ]
})
watch(() => props.modelValue, () => {
  form.value = props.modelValue
}, {
  deep: true
})

watch(form.value, () => {
  emit('update:modelValue', form.value)
})
</script>

<style scoped lang="scss">

</style>
